//
//  CXPresentationController.swift
//  CXWeibo
//
//  Created by 陈旭 on 16/9/11.
//  Copyright © 2016年 陈旭. All rights reserved.
//

import UIKit

class CXPresentationController: UIPresentationController {
    // MARK:- 属性
    var presentedFrame : CGRect = CGRectZero
    // MARK:- 懒加载属性
    private lazy var coverView: UIView = UIView()
    // MARK:- 系统回调函数
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        // 设置弹出view的尺寸
        presentedView()?.frame = presentedFrame
        // 设置蒙版
        setupCoverView()
    }
}

// MARK:- 设置UI界面
extension CXPresentationController {
    /// 设置蒙版
    private func setupCoverView(){
        containerView?.insertSubview(coverView, atIndex: 0)
        coverView.backgroundColor = UIColor(white: 0.5, alpha: 0.2)
        coverView.frame = containerView!.bounds
        let tap = UITapGestureRecognizer(target: self, action: "coverViewClick")
        coverView.addGestureRecognizer(tap)
    }
}

// MARK:- 事件监听
extension CXPresentationController {
    /// 点击蒙版
    @objc private func coverViewClick(){
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}